/*
 * Copyright 2010-2012 Susanta Tewari. <freecode4susant@users.sourceforge.net>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package coalescent.br.comps;

import coalescent.br.AMCElement;
import coalescent.br.BRWCompInfo;

import java.util.ArrayList;
import java.util.List;

/**
 * Builds all the sample configurations ancestral to a sample configuration (in backwards order) by
 * observing its recursion (via post-order implementation).
 *
 * @author Susanta Tewari
 * @version 1.0 Dec 3, 2010
 *
 * @param <E>
 * @param <S>
 */
public class BRWACBuilder<E extends Enum<E>, S extends AMCElement<E, S>>
        extends BRWCompAdapter<E, S> {

    /**
     * the final result of the computation. Holds all the genealogies of the statistic.
     */
    private List<S> ancestralConfigs = new ArrayList<>();

    /**
     * All sample configurations ancestral to the sample configuration being observed. The
     * ancestra configurations are ordered backwards.
     *
     * @return ancestral configurations for the sample configuration being observed
     */
    public List<S> ancestralConfigs() {
        return ancestralConfigs;
    }

    /**
     * Method description
     *
     *
     * @param event
     */
    @Override
    public void onBoundaryCondn(BRWCompInfo<E, S> event) {
        ancestralConfigs.add(event.getCurrentConfig());
    }

    /**
     * Method description
     *
     *
     * @param event
     */
    @Override
    public void onFinishedIteratingEvents(BRWCompInfo<E, S> event) {
        ancestralConfigs.add(event.getCurrentConfig());
    }

    /**
     * Method description
     *
     *
     * @return
     */
    @Override
    public String toString() {

        super.toString();

        StringBuilder builder = new StringBuilder();

        builder.append("Ancestral Configurations:\n");

        for (AMCElement config : ancestralConfigs) {

            builder.append(config + "\n");
        }

        builder.deleteCharAt(builder.length() - 1);

        return builder.toString();
    }
}
